package controler;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.HoadonModel;
import model.KhachhangModel;

import entity.Hoadon;
import entity.khachhang;

/**
 * Servlet implementation class UserDetail
 */
@WebServlet("/UserDetail")
public class UserDetail extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserDetail() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @throws SQLException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    public void laydshoadon(HttpServletRequest request, HttpServletResponse response) throws SQLException{
    	HoadonModel model = new HoadonModel();
    	List<Hoadon> listhd = new ArrayList<Hoadon>();
    	HttpSession session = request.getSession();
    	String strSearch = "select HOADON.MAHD ,HOTEN, TENSP,CTHD.SOLUONG,DONGIA,HOADON.TRANGTHAI from HOADON,CTHD,SANPHAM,KHACHHANG where HOADON.MAHD= CTHD.MAHD and CTHD.MASP = SANPHAM.MASP and KHACHHANG.MAKH = HOADON.MAKH and TENDANGNHAP = '"+session.getAttribute("userName")+"'";
    	listhd = model.laycthd(strSearch);
    	request.setAttribute("listhd", listhd);
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws SQLException{
    	response.setContentType("text/html;charset=UTF-8");
    	String makh = request.getParameter("makh");
    	List<khachhang> listkh = new ArrayList<khachhang>();
    	KhachhangModel model = new KhachhangModel();
    	listkh = model.timkhachhang("select * from KHACHHANG where TENDANGNHAP = '"+makh+"'");
    	request.setAttribute("listkh", listkh);
    }
    
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
